import numpy as np
arr = np.array([[1.5, 2.5, 3, 4, 5],
                [2, 3, 4, 5.3, 6.7]])
print(arr)
print("原数组数据类型", arr.dtype)

arr1 = arr.astype(np.float16)
print(arr1)

print("----------------------------------------")
import numpy as np
arr = np.array([[1, 2, 3, 4],
                [2, 3, 4, 5],
                [6, 7, 8, 9]])
print(arr)
print("原数组形状", arr.shape)

arr1 = arr.reshape((2, 6))  # 和原数组size保持一致
print(arr1)
print("新数组形状", arr1.shape)

arr.resize((2, 6))          # 就地修改，原数组改变了
print(arr)

print("------------------------------------")
import numpy as np
arr = np.array([[1, 2, 3, 4],
                [2, 3, 4, 5],
                [6, 7, 8, 9]])

arr1 = arr.flatten()
print("展平为副本", arr1)
arr1[1] = 666
print(arr)

arr2 = arr.ravel()
print("展平为视图", arr2)
arr2[1] = 888
print(arr)
print("------------------------")

import numpy as np
arr = np.array([[1, 2, 3, 4],
                [2, 3, 4, 5],
                [6, 7, 8, 9]])

# 矩阵/二维数组转置，行列互换
arr3 = arr.T
print("转置后数组\n", arr3)

print("="*40)

arr1 = np.array([
    [[1, 2, 3, 4],
     [12, 13, 14, 5],
     [6, 7, 8, 9]],
    [[10, 20, 30, 40],
     [22, 32, 42, 52],
     [16, 17, 18, 19]]
])
print(arr1)
print("原三维数组形状", arr1.shape)
arr2 = arr1.swapaxes(0, 2)
print(arr2)
print("交换轴0和轴2后的形状是", arr2.shape)
print("===========================================")
np.random.seed()
np.random.seed(0)
np.random.seed(42)


print("====================================")
import numpy as np
# 设置随机数种子，复现结果
np.random.seed(0)

arr1 = np.random.rand(10)
print("[0, 1)均匀分布", arr1)

arr2 = np.random.randn(10000)
# print("标准正态分布", arr2)
print("均值", np.mean(arr2))
print("标准差", np.std(arr2))

arr3 = np.random.normal(1, 3, 10000)
# print("指定参数的正态分布", arr3)
print("指定均值", np.mean(arr3))
print("指定标准差", np.std(arr3))

arr4 = np.random.randint(10, 20, (2, 3))
print("随机整数数组", arr4)

data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
np.random.shuffle(data)
print("随机打乱顺序", data)


import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr + 5)
print(arr - 1)
print(arr * 10)
print(arr / 0.5)
print("==================================")
import numpy as np

arr1 = np.array([[1, 2, 3],
                 [4, 5, 6]])

arr2 = np.array([10, 20, 30])
print(arr1 + arr2)

arr3 = np.array([[100],
                 [200]])
# shape(2, 3) + shape(3, ) + shape(2, 1)
print(arr1 + arr2 + arr3)

ret = np.add(np.add(arr1, arr2), arr3)
print(ret)
print("===========================")
import numpy as np

A = np.array([[1, 2, 3, 4],
              [2, 3, 4, 5],
              [3, 4, 5, 6]])
B = np.array([10, 20, 30, 40])
print(A.shape)
print(B.shape)

print(A + B)    # 运算成功

C = np.array([[1, 2, 3]])
D = np.array([[10],
              [20],
              [30]])
print(C.shape)
print(D.shape)
print(C + D)    # 运算成功

E = np.array([[1, 2, 3],
              [2, 3, 4]])
F = np.array([[10],
              [20],
              [30]])
print(E.shape)
print(F.shape)


import numpy as np
print("=============")
arr = np.zeros((3, 1, 2, 1))
print("原数组形状", arr.shape)

# 去除维度为1的形状
arr1 = np.squeeze(arr)
print("去除维度为1的形状后", arr1.shape)
print("=============")
arr2 = np.squeeze(arr, axis=1)
print("去除指定位置且维度为1的形状后", arr2.shape)
print("=============")
# 在指定位置添加维度为1的形状
arr3 = np.expand_dims(arr, axis=0)
print("指定位置添加维度为1的形状后", arr3.shape)


print("------------------------------------")
import numpy as np

A = np.array([[1, 2], [3, 4]])      # 2*2
B = np.array([[5, 6]])              # 1*2
# - concatenate()：沿指定轴连接多个数组
C = np.concatenate((A, B), axis=0)
print(C.shape)
C = np.concatenate((A, B.T), axis=1)
print(C.shape)

# - stack()：沿新轴堆叠数组
A = np.array([[1, 2, 3], [4, 5, 6]])    # 2*3
B = np.array([[5, 6, 7], [5, 6, 8]])    # 2*3
S = np.stack((A, B))
print(S.shape)

# - hstack() vstack()：沿水平或垂直方向堆叠数组
H = np.hstack((A, B))
print(H.shape)
V = np.vstack((A, B))
print(V.shape)

# - column_stack(): 按列堆叠数组
CS = np.column_stack((A, B))
print(CS.shape)
print("================================================")
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])
# - split()        沿指定轴分割数组
# indices_or_sections
# 该参数若只设置一个值，会按照该值均分数组，要求：该值必须能被数组长度整除
arr1 = np.split(arr, 2)
print(arr1)
# 该参数若设置的是[]，则按【】值进行分割
arr2 = np.split(arr, [2, 3, 10])
print(arr2)


# - hsplit() vsplit()    沿水平或垂直方向分割数组
narr = np.reshape(arr, (2, 3))
arr3 = np.hsplit(narr, [2,])
print(arr3)


# - array_split()    不完全平均分割数组
arr4 = np.array_split(arr, 4)
print(arr4)


import numpy as np

arr = np.array([10, 8, 7, 1, 3, 2, 4, 6, 5])
print(np.mean(arr), np.max(arr))
print(np.var(arr), np.std(arr))
print("最大值索引", np.argmax(arr))
print("最小值索引", np.argmin(arr))
# 序列个数为偶数，会取中间两个值计算平均
# 序列个数为奇数，直接取中间值
print("中位数", np.median(arr))

arr = np.array([[2, 3, 1, 4],
                [1, 0, 4, 3],
                [0, 3, 1, 2]])
ret = np.max(arr)
print(ret)  # 输出 4
ret = np.max(arr, axis=0)
print(ret)
ret = np.max(arr, axis=1)
print(ret)

# 在统计计算中，若指定axis，那么输出形状中该轴形状消失
arr = np.array([
    [[2, 3, 1, 4],
    [1, 0, 4, 3],
    [0, 3, 1, 2]],

    [[2, 3, 1, 4],
    [1, 0, 4, 3],
    [0, 3, 1, 2]]
])
print(arr.shape)        # (2, 3, 4)

ret = np.max(arr, axis=1)
print(ret.shape)        # (2, 4)


import numpy as np

arr = np.array([10, 8, 7, 1, 3, 2, 4, 6, 5])
ret = arr[arr > 5]
print(ret)  # [10  8  7  6]

ret = np.where(arr > 5)
print(ret)  # (array([0, 1, 2, 7], dtype=int64),)

# np.where(arr > 5, "True", "False") 类似于三元运算符
# 判断条件arr>5 满足执行"True" 不满足执行"False"
ret = np.where(arr > 5, "True", "False")
print(ret)  # ['True' 'True' 'True' 'False' 'False' 'False' 'False' 'True' 'False']

ret = np.where(arr > 5, arr*2, arr)
print(ret)  # [20 16 14  1  3  2  4 12  5]

import numpy as np

arr = np.array([10, 8, 7, 1, 3, 2, 4, 6, 5])
arr1 = np.sort(arr)
print(arr1)

arr = np.array([[2, 3, 4, 1],
                [1, 0, 4, 3],
                [0, 3, 2, 1]])
arr1 = np.sort(arr, axis=0)
print(arr1)


import numpy as np

A = np.array([[1, 2, 3],
              [4, 5, 6]])

B = np.array([[7, 8],
              [9, 10],
              [11, 12]])

print("点积dot\n", np.dot(A, B))
print("运算符@\n", A @ B)

import numpy as np
# 一维数组的 内积
A = np.array([1, 3, 2, 4])
B = np.array([0, 3, 5, 1])
print(np.inner(A, B))   # 输出 23

# 二维数组 内积
print(np.inner(A.reshape(2, 2),
               B.reshape(2, 2)))
"""
输出
[[ 9  8]
 [12 14]]
"""

import numpy as np

A = np.array([1, 2, 3])     #(3, )
B = np.array([4, 5, 6])     #(3, )
print("一维数组的外积\n", np.outer(A, B))
# 计算规则是：转置 扩维 广播 相乘计算

A = np.array([[1, 2],
              [3, 4]])
B = np.array([[5, 6],
              [7, 8]])
print("二维数组外积\n", np.outer(A, B))
# 计算规则：展平 转置 扩维 广播 相乘计算

A = np.array([[[1, 2],
               [3, 4]],
              [[5, 6],
               [7, 8]]
              ])
B = np.array([1, 1, 1, 1, 1, 1, 1, 1]).reshape(2, 2, 2)

print("三维数组外积\n", np.outer(A, B))